package com.wen.wenda.controller;

import com.wen.wenda.common.BaseCode;
import com.wen.wenda.common.BaseResponse;
import com.wen.wenda.common.utils.ReturnUtil;
import com.wen.wenda.exception.BusinessException;
import com.wen.wenda.model.domain.ScoringResult;
import com.wen.wenda.model.domain.UserAnswer;
import com.wen.wenda.model.request.answer.UserAnswerCreateRequest;
import com.wen.wenda.model.request.answer.UserAnswerSearchRequest;
import com.wen.wenda.model.request.answer.UserAnswerUpdateRequest;
import com.wen.wenda.model.request.scoring.ScoringResultCreateRequest;
import com.wen.wenda.model.request.scoring.ScoringResultUpdateRequest;
import com.wen.wenda.model.vo.ScoringResultVO;
import com.wen.wenda.model.vo.UserAnswerVO;
import com.wen.wenda.service.ScoringResultService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 评分结果接口
 *
 * @author wen
 */
@RestController
@RequestMapping("/scoring")
public class ScoringResultController {

    @Resource
    private ScoringResultService scoringResultService;

    @PostMapping("/create")
    public BaseResponse<Boolean> createScoringResult(@RequestBody ScoringResultCreateRequest scoringResultCreateRequest, HttpServletRequest request) {
        // 判断参数是否为空
        if (scoringResultCreateRequest == null || request == null) {
            throw new BusinessException(BaseCode.PARAMS_NULL_ERROR);
        }
        // todo 具体的添加逻辑
        ScoringResult scoringResult = new ScoringResult();
        BeanUtils.copyProperties(scoringResultCreateRequest, scoringResult);
        boolean save = scoringResultService.save(scoringResult);
        return ReturnUtil.success(save);
    }

    @PostMapping("/delete")
    public BaseResponse<Boolean> deleteScoringResult(@RequestBody long id, HttpServletRequest request) {
        if (request == null) {
            throw new BusinessException(BaseCode.PARAMS_NULL_ERROR);
        }
        boolean removedById = scoringResultService.removeById(id);
        if (!removedById) {
            throw new BusinessException(BaseCode.RESOURCE_NOT_FOUND, "删除失败");
        }
        return ReturnUtil.success(true);
    }

    @PostMapping("/update")
    public BaseResponse<Boolean> updateScoringResult(@RequestBody ScoringResultUpdateRequest scoringResultUpdateRequest, HttpServletRequest request) {
        // 判断参数是否为空
        if (scoringResultUpdateRequest == null || request == null) {
            throw new BusinessException(BaseCode.PARAMS_NULL_ERROR);
        }
        // todo 具体的修改逻辑
        ScoringResult scoringResult = scoringResultService.getById(scoringResultUpdateRequest.getId());
        BeanUtils.copyProperties(scoringResultUpdateRequest, scoringResult);
        boolean res = scoringResultService.updateById(scoringResult);
        return ReturnUtil.success(res);
    }

    @GetMapping("/list")
    public BaseResponse<List<ScoringResultVO>> getScoringResultList(UserAnswerSearchRequest userAnswerSearchRequest, HttpServletRequest request) {
        if (userAnswerSearchRequest == null || request == null) {
            throw new BusinessException(BaseCode.PARAMS_NULL_ERROR);
        }
        // todo 具体的查询校验逻辑
        List<ScoringResult> list = scoringResultService.list();
        List<ScoringResultVO> scoringResultVOList = list.stream().map(item -> {
            ScoringResultVO scoringResultVO = new ScoringResultVO();
            BeanUtils.copyProperties(item, scoringResultVO);
            return scoringResultVO;
        }).toList();
        return ReturnUtil.success(scoringResultVOList);
    }
}
